// 根据token或者其它的判断条件来实现
import { useRoutes, useNavigate, useLocation } from "react-router-dom"
import { useEffect } from "react"

const useRoutesWithGuard = (key, routes) => {
    let token = localStorage.getItem(key);
    let Element = () => useRoutes(routes)
    let navigate = useNavigate()
    let location = useLocation()
    // console.log(location)
    useEffect(() => {
        if (!token) { // 没有token,重定向到login
            // navigate("/login")
            if (location.pathname !== "/login") {
                navigate("/login")
            }
        }
    })

    //调用useEffect

    return Element
}

export default useRoutesWithGuard